<template>
  <div class="update-gender">
    <van-picker
      title="性别"
      show-toolbar
      :columns="columns"
      :default-index="defaultIndex"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      @change="onChange"
    />
  </div>
</template>

<script>
// 导入修改个人资料请求接口
import { setUserModifyInfo } from '@/api/users'
export default {
  name: 'UpdateGender',
  props: {
    value: {
      type: [String, Number],
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      defaultIndex: this.value
    }
  },
  methods: {
    // 确认事件
    async onConfirm (value, index) {
      this.$toast.loading({
        messages: '更改中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await setUserModifyInfo({
          gender: this.defaultIndex
        })
        this.$emit('input', this.defaultIndex)
        this.$emit('close')
        this.$toast.success('性别已生效！')
      } catch (err) {
        this.$toast.fail('修改失败了！')
      }
    },
    // 改变事件
    onChange (picker, value, index) {
      this.defaultIndex = index
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.van-picker-column__item {
  font-weight: 600;
}
</style>
